﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TaoSH.AppCode;
using System.Data;
using TaoSH.Data;
using Newtonsoft.Json;
namespace TaoSH.View
{
    public partial class SelList : BasePage
    {
        public string strFormCode
        {
            get { return Request.QueryString["FormCode"] ?? ""; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {


            JH_Auth_FormBase FormBaseData = new JH_Auth_FormBaseB().GetEntity(d => d.FormCode == strFormCode);
            string strQueryName = "../../View/UCList/" + FormBaseData.FormViewUrl;
            QueryUControl Control = LoadControl(strQueryName) as QueryUControl;
            Control.ID = "SelQueryControl";

            QueryCon.Controls.Add(Control);


            Button btQuery = Control.FindControl("btQuery") as Button;
            btQuery.Click += new EventHandler(this.btQuery_Click);

            if (!IsPostBack)
            {
                querydata(Control.SetinitQuery() + Control.SetDefaultQuery());
                hidQueryWhere.Value = Control.SetinitQuery() + Control.SetDefaultQuery();
            }
            
        }


        public void querydata(string strWhere)
        {
            string strReturn = "";
            int intPageLen = int.Parse(hidPageLen.Value);
            int intIndex = int.Parse(ddlPageIndex.SelectedValue);
            int intPageStart = (intIndex - 1) * intPageLen;
            int intPageEnd = intIndex * intPageLen;


            JH_Auth_FormBase FormBaseData = new JH_Auth_FormBaseB().GetEntity(d => d.FormCode == strFormCode);

            string strFormTable = FormBaseData.FormTable;
            if (!string.IsNullOrEmpty(FormBaseData.FormGetSQL))
            {
                strFormTable = " (" + FormBaseData.FormGetSQL + ") as Form ";
            }


            if (strFormCode=="ZPXQ")
            {
                strWhere = strWhere + " AND STATE=3 AND Deleted=0 ";
            }
            if (strFormCode == "SPXQ")//审核通过，上架，未删除
            {
                strWhere = strWhere + " AND CheckState=1 AND Deleted=0 AND IsSelf =1";
            }

            DataTable dtResult = new DataTable();
            dtResult = new JH_Auth_FormBaseB().GetDTByCommand("  select * from ( select row_number() over(order by  ID DESC ) as RowNum, * from  " + strFormTable + " where 1=1 " + strWhere + "  ) t  where   t.RowNum between " + intPageStart + " and " + intPageEnd);
            strReturn = JsonConvert.SerializeObject(dtResult);




            tabledata.Value = strReturn;
            hidDataCount.Value = GetQueryCount(FormBaseData, strWhere);
            double PageCount = Math.Ceiling(double.Parse(hidDataCount.Value) / double.Parse(hidPageLen.Value));




            new CommonHelp.BindData().BindPage(ddlPageIndex, PageCount.ToInt32());
            if (ddlPageIndex.Items.Contains(new ListItem() { Text = intIndex.ToString(), Value = intIndex.ToString() }))
            {
                ddlPageIndex.SelectedValue = intIndex.ToString();
            }
            else
            {
                ddlPageIndex.SelectedValue = "1";
            }


        }

        protected void ddlPageIndex_SelectedIndexChanged(object sender, EventArgs e)
        {
            querydata(hidQueryWhere.Value);
        }

        public string GetQueryCount(JH_Auth_FormBase FormBaseData, string strWhere)
        {
            string strReturn = "";
            string strFormTable = FormBaseData.FormTable;
            if (!string.IsNullOrEmpty(FormBaseData.FormGetSQL))
            {
                strFormTable = " (" + FormBaseData.FormGetSQL + ") as Form ";
            }
            DataTable dt = new DataTable();
            dt = new JH_Auth_FormBaseB().GetDTByCommand("select count(*) as Datacount from ( select row_number() over(order by  ID DESC ) as RowNum, * from  " + strFormTable + " where 1=1 " + strWhere + "  ) t ");
            strReturn = dt.Rows[0][0].ToString();
            return strReturn;

        }
        void btQuery_Click(Object sender, EventArgs e)
        {
            UCQueryData();
        }


        public void UCQueryData()
        {
            QueryUControl Control = QueryCon.FindControl("SelQueryControl") as QueryUControl;
            string strQuery = Control.SetinitQuery() + Control.GetQuerydata();
            hidQueryWhere.Value = strQuery;
            ddlPageIndex.SelectedValue = "1";//初始化为第一页结果
            hidselID.Value = "";//初始化选择为空
            querydata(strQuery);
        }
    }
}